// pages/index/drug/detail/index.js
const App = getApp();
const AJAX = require("../../../../common/request")
const API = require("../../../../common/api")
const common = require("../../../../common/common")
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		navTop: 0,
		navContentHeight: 0,
		placeholderHeight: 0,
		seachText: "",
		options: {},
		UPLOADURL: '',
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.setData({
			navTop: App.globalData.navTop,
			navContentHeight: App.globalData.navContentHeight,
			UPLOADURL: API.UPLOADURL,
			APPLETSTATUS: App.globalData.APPLETSTATUS
		})
		const query = wx.createSelectorQuery();
		query.select('.head').boundingClientRect();
		query.exec((res) => {
			this.setData({
				placeholderHeight: res[0].height
			})
		});
		AJAX.POST("/system/AppWechat/getbDrugIntroductionlist", {
			id: options.id,
			wechatUserId: App.globalData.user.wechatUserId,
		}).then((res) => {
			this.setData({
				options: res.rows[0]
			})
		})
	},
	goBack() {
		const pages = getCurrentPages();
		//获取不到上级返回首页
		if (pages.length > 1) {
			wx.navigateBack({})
		} else {
			wx.reLaunch({
				url: '/pages/index/index',
			})
		}
	},
	// 收藏
	clickCollect(e) {
		if (this.data.options.scNum > 0) {
			return
		}
		//判断缓存手机号是否存在 
		if (!App.globalData.user.wxLoginPhone) {
			wx.showToast({
				title: '尚未登录',
				icon: "none"
			})
			wx.navigateTo({
				url: '/pages/login/index',
			})
			return false;
		}
		// 检查登录状态
		common.checkLogin().then((loginRes) => {
			if (!loginRes) {
				return wx.showToast({
					title: '尚未登录，收藏失败',
					duration: "3000",
					icon: "none"
				})
			}
			AJAX.POST("/system/AppWechat/addbCollect", {
				wechatUserId: App.globalData.user.wechatUserId,
				drugIntroductionId: e.currentTarget.dataset.id,
			}).then(res => {
				wx.showToast({
					title: '药品收藏成功',
				})
				this.setData({
					[`options.scNum`]: 1
				})
			})
		})
	},
	goSearch() {
		wx.redirectTo({
			url: '/pages/index/search/index',
		})
	},
	onShareAppMessage(options) {
		// 默认分享配置
		return {
			title: App.globalData.onShareAppMessage.title,
			path: App.globalData.onShareAppMessage.path,
			imageUrl: App.globalData.onShareAppMessage.imageUrl,
		}
	},

})